<!--
 * @Description:
 * @Author: cgt
 * @Date: 2021-12-23 14:05:56
 * @LastEditors: cgt
 * @LastEditTime: 2021-12-28 20:03:26
-->
<template>
  <div>
    <el-button @click="show">显示</el-button>
    <el-button @click="hide">隐藏</el-button>
    <el-switch v-model="showText" active-text="显示标注文字" inactive-text="隐藏标注文字" @change="handleCheckChange"></el-switch>
    <select-check-tool :options="options" @change="changeStyle"></select-check-tool>
  </div>
</template>

<script>
import mixins from '../mixins';
import SelectCheckTool from '../SelectCheckTool/SelectCheckTool.vue';
export default {
  name: 'DispatchGridTool',
  components: {
    SelectCheckTool
  },
  props: {
    getMap: {
      type: Function,
      required: true
    },
    getTool: {
      type: Function,
      required: true
    },
    mapLoadSuccess: Boolean
  },
  mixins: [mixins],
  data() {
    return {
      styleChanged: false,
      showText: true,
      options: [{
        sbzlxList: ['30000000'],
        show: true,
        expression: ['get', 'sbmc'],
        name: '变电站名称',
        value: 1
      }, {
        sbzlxList: ['20100000', '10100000', '10000000'],
        show: true,
        expression: [
          'get',
          [
            'get',
            ['to-string', ['get', 'dydj']],
            ['literal', {
              '25': 'sbmc',
              '32': 'sbmc',
              '33': 'sbmc',
              '35': 'sbmc',
              '37': 'sbmc',
              '85': 'sbmc'
            }]
          ]
        ],
        name: '高压线路名称',
        value: 2
      }, {
        sbzlxList: ['20100000', '10100000'],
        show: true,
        expression: [
          'get',
          [
            'get',
            ['to-string', ['get', 'dydj']],
            ['literal', {
              '21': 'sbmc',
              '22': 'sbmc',
              '24': 'sbmc'
            }]
          ]
        ],
        name: '中压线路名称',
        value: 3
      }, {
        sbzlxList: [
          '30000005',
          '32300000',
          '11000000',
          '11000002',
          '11000001'
        ],
        show: true,
        expression: ['get', 'sbmc'],
        name: '配电设备名称',
        value: 4
      }, {
        sbzlxList: [
          '30000004',
          '32400000',
          '20400000',
          '11100000',
          '11200000'
        ],
        show: true,
        expression: ['get', 'sbmc'],
        name: '开关设备名称',
        value: 5
      }, {
        sbzlxList: [
          '30000003',
          '30000010',
          '30000002',
          '30000099',
        ],
        show: true,
        expression: ['get', 'sbmc'],
        name: '电厂名称',
        value: 6
      }]
    };
  },
  watch: {
    mapLoadSuccess(success) {
      if (success) {
        this.setOption();
      }
    }
  },
  methods: {
    show() {
      this.getTool('DispatchGridTool').show(true, { maxWidth: 1.8 });
    },
    hide() {
      this.getTool('DispatchGridTool').hide();
    },
    setOption() {
      this.getTool('DispatchGridTool').setSearchOption(Object.assign({ ssdw: '四川省' }, this.option));
    },
    handleCheckChange(value) {
      console.log(value);
      this.getTool('LabelControlTool').changeTextState(value);
    },
    changeStyle(selected) {
      var config = [];
      console.log(selected);
      var values = selected.map((k) => k.value);
      config = selected.filter((k) => k.value !== 2 && k.value !== 3).slice(0);
      if (values.indexOf(2) >= 0 && values.indexOf(3) >= 0) {
        var obj = {};
        [21, 22, 24, /**高压*/25, 32, 33, 35, 37, 85].forEach((dydj) => {
          obj[dydj + ''] = 'sbmc';
        });
        // 线路都选择
        config.push({
          ...selected.find((k) => k.value === 2),
          expression: [
            'get',
            [
              'get',
              ['to-string', ['get', 'dydj']],
              ['literal', obj]
            ]
          ]
        });
      } else {
        config.push(
          ...selected.filter((k) => k.value === 2 || k.value === 3).slice(0)
        );
      }
      var labelFilter = (layerid) => {

        var found = config.find((item) => {
          return item.sbzlxList.some((sbzlx) => layerid.indexOf(sbzlx) >= 0);
        });
        if (found) {
          return found.expression;
        }
        return '';

      };
      if(this.mapLoadSuccess) {
        this.getMap().getGridTool().setLabelFilter(labelFilter);
      }
    }
  },
  mounted() { }
};
</script>

<style scoped>

</style>
